package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pogo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsService {

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setCounts(0);
        pageResult.setPages(0);

        PageInfo<Comment> commentPageInfo = this.quanZiApi.queryCommentList(publishId, page, pageSize);
        List<Comment> records = commentPageInfo.getRecords();

        if (CollectionUtils.isEmpty(records)){
            return pageResult;
        }



        //查询评论人信息
        List<Long> userIds=new ArrayList<>();
            //record中的所有评论人信息
        for (Comment record : records) {
            //去重userIs不需重复
            if (!userIds.contains(record.getUserId())){
                userIds.add(record.getUserId());
            }
        }
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("user_id",userIds);//userIds无重复
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);

        //return值 评论列表
        List<Comments> commentList=new ArrayList<>();

        for (Comment record : records) {

            //单条评论
            Comments comments = new Comments();
            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            comments.setContent(record.getContent());

            //填充用户相关信息
            for (UserInfo userInfo : userInfoList) {
                if (record.getUserId().longValue()==userInfo.getUserId().longValue()){

                    comments.setNickname(userInfo.getNickName());
                    comments.setAvatar(userInfo.getLogo());
                    break;
                }
            }


            //是否已点赞
            Long id = UserThreadLocal.get().getId();
            String userHasLikeKey= "QUANZI_COMMENT_LIKE_USER_"+id+"_"+comments.getId();
            comments.setHasLiked(this.redisTemplate.hasKey(userHasLikeKey)?1:0);

            //点赞数
            String likeCommentKey="QUANZI_COMMENT_LIKE_"+comments.getId();
            String likeCount=this.redisTemplate.opsForValue().get(likeCommentKey);
            if (StringUtils.isNotEmpty(likeCount)){
                comments.setLikeCount(Integer.valueOf(likeCount));
            }else {
                comments.setLikeCount(0);
            }

            //填充好得comments加入commentList列表中
            commentList.add(comments);
        }

        pageResult.setItems(commentList);
        return pageResult;
    }


    //保存动态评论
    public boolean saveComments(String movementId, String content) {
        return this.quanZiApi.saveComment(UserThreadLocal.get().getId(), movementId, 2, content);
    }
}
